/*-------------------------------------------------------------------------*/
/**
  @file		initial.c
  @author	Renaud Wanchoor
  @date	July 2004
  @version	$Revision: 2.9 $
  @brief	initial partitioning 
  @update date	March 2007 by Xiaochen Wu
  
*/
/*--------------------------------------------------------------------------*/
#include "pagrid.h"
/**
 * \brief Initial partitioning
 *
 * Find the processor assignment that minimizes the total execution time
 * of the application, the number of vertices in the graph must be equal
 * to the number of processors
 *
 * \param graph application graph structure to partition
 * \param grid grid information structure
 */
void InitialPartExec(GraphType* graph,GridType* grid){
  int i,j,v,w;
  int prov, prow;
  int istart, iend;

  int       npro   = grid->npro;
  double** ratio   = grid->ratio;
  double   *exec   = grid->exec;
  double* prospeed = grid->prospeed;
  int*    adjidx   = graph->adjidx;
  int*    adjncy   = graph->adjncy;
  int*    adjwgt   = graph->adjwgt;
  int*      vwgt   = graph->vwgt;
  int*       pro   = graph->pro;
  int*      rpro   = grid->rpro;

  
  /**********************************/
  /* Init                           */
  /**********************************/
  /* random initial partitionning */
  randnum(npro,pro);

  for(i=0;i<npro;i++)
    rpro[pro[i]] = i;

  /* compute exec time */
  for(v=0;v<npro;v++){
    prov = pro[v];
    exec[prov] = vwgt[v]*prospeed[prov];
    for(j=adjidx[v];j<adjidx[v+1];j++){
      w = adjncy[j];
      exec[prov] += adjwgt[j] * ratio[prov][pro[w]];
    }
  }

  /****************************************/
  /* Refinement                           */
  /****************************************/
  /* copy current graph it is the same as the processor connectivity for the application */
  IniProcessorCon(grid,graph);

  int*    app_adjidx = grid->app->adjidx;
  int*    app_adjncy = grid->app->adjncy;
  int*       app_pro = grid->app->pro;
  int**      latency = grid->latency;
  for(v = 0; v < npro; v++){
    istart = app_adjidx[v];
    iend = app_adjidx[v+1];
    prov = app_pro[v];
    for(i = istart; i < iend; i++){
      w = app_adjncy[i];
      prow = app_pro[w];
      exec[prov] += latency[prov][prow];
    }
  }
}

